package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.CancelNursingTaskDTO;
import com.zzyl.nursing.dto.NursingQueryDTO;
import com.zzyl.nursing.dto.NursingTaskExecuteDTO;
import com.zzyl.nursing.dto.NursingTaskTimeUpdateDTO;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.NursingTaskDetailVO;
import com.zzyl.nursing.vo.NursingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理任务Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-17
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private IElderService elderService;

    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTaskDetailVO selectNursingTaskById(Long id)
    {
        NursingTask byId = getById(id);
        NursingTaskDetailVO vo = BeanUtil.toBean(byId, NursingTaskDetailVO.class);//1.先转存信息给vo
        vo.setAge(elderService.lambdaQuery().eq(Elder::getId,vo.getElderId()).one().getAge());//2.赋值年龄
        String s =nursingTaskMapper.selectNuringName(id);
        String[] split = s.split(",");
        vo.setNursingName(new ArrayList<String>());
        List<String> list =new ArrayList<>();
        for (String string : split) {
            vo.getNursingName().add(string);//3.赋值nursing_name
        }
        //4.查询护理等级
        String level=nursingTaskMapper.selectNursingLevelName(id);
        vo.setNursingLevelName(level);
        vo.setUpdater("若依");//至于若依这个字段,我是真没搞懂去哪里拿,而且这个功能前端是不是有错误啊???
        return vo;
    }

    /**
     * 查询护理任务列表
     * 
     * @param dto 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTaskVo> selectNursingTaskList(NursingQueryDTO dto)
    {
        List<NursingTaskVo> vos=nursingTaskMapper.selectNursingTaskList(dto);
        for (NursingTaskVo vo : vos) {
            String[] split = vo.getNursing().split(",");
            vo.setNursingName(new ArrayList<String>());
            for (String s : split) {
                vo.getNursingName().add(s);
            }
            vo.setNursing(null);
        }
        //错误思路:
                //错误原因:将从数据库拿到的分页数据转换成另一个集合,结果导致只能封装十条数据,从而出现错误
//        //对于nursing_name字段,先用字符串来接,再转成数组
//        List<NursingTaskMapperVo> nursingTaskMapperVo=nursingTaskMapper.selectNursingTaskList(dto);
//         List<NursingTaskVO> list =new ArrayList<>();
//            for (NursingTaskMapperVo taskMapperVo : nursingTaskMapperVo) {
//                NursingTaskVO bean = BeanUtil.toBean(taskMapperVo, NursingTaskVO.class);//除了nursingname字段,其他值全部复制进去
//                //复制nursing_name的值
//                String[] split = taskMapperVo.getNursingname().split(",");//分割成数组
//                bean.setNursingName(new ArrayList<String>());//初始化每个护理任务中的nursing_name属性
//                for (String s : split) {//遍历数组,将每个护理员工复制进nursingName数组里面
//                    bean.getNursingName().add(s);//将护理员工添加进集合里面
//                }
//                list.add(bean);
//            }
//         return list;
        System.out.println(vos);
        return vos;
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void cancelTask(CancelNursingTaskDTO dto) {
        //还是都加上判断吧
        NursingTask one = lambdaQuery().eq(NursingTask::getId, dto.getTaskId()).one();
        if(one==null){
            throw new RuntimeException("该条任务数据不存在");
        }
        //还是一行代码搞定,注意将status改为3
        update(new LambdaUpdateWrapper<NursingTask>().set(NursingTask::getCancelReason,dto.getReason()).set(NursingTask::getStatus,3).eq(NursingTask::getId,dto.getTaskId()));
    }

    @Override
    public void doTask(NursingTaskExecuteDTO dto) {
        //还是都加上判断吧
        NursingTask one = lambdaQuery().eq(NursingTask::getId, dto.getTaskId()).one();
        if(one==null){
            throw new RuntimeException("该条任务数据不存在");
        }
        //还是一行代码搞定,注意将status改为2
        update(new LambdaUpdateWrapper<NursingTask>().set(NursingTask::getEstimatedServerTime,dto.getEstimatedServerTime()).set(NursingTask::getMark,dto.getMark()).set(NursingTask::getTaskImage,dto.getTaskImage()).set(NursingTask::getStatus,2).eq(NursingTask::getId,dto.getTaskId()));
    }

    @Override
    public void alterTask(NursingTaskTimeUpdateDTO dto) {
        //还是都加上判断吧
        NursingTask one = lambdaQuery().eq(NursingTask::getId, dto.getTaskId()).one();
        if(one==null){
            throw new RuntimeException("该条任务数据不存在");
        }
        //还是一行代码搞定
        update(new LambdaUpdateWrapper<NursingTask>().set(NursingTask::getEstimatedServerTime,dto.getEstimatedServerTime()).eq(NursingTask::getId,dto.getTaskId()));
    }
}
